<<<<<<< HEAD
<?php

namespace app\api\controller\store;

use app\api\services\CacheServices;
use app\api\services\store\OrderServices;
use app\api\validate\SubmitOrderValidate;
use app\common\controller\Api;
use think\Db;
use think\Queue;

class Order extends Api
{
    protected $noNeedRight = '*';

    public function submit()
    {

        $params = input();

        (new SubmitOrderValidate())->goCheck();
        $lockKey = sprintf("order_submit_%s_%s", $this->auth->id, md5(serialize($params)));
        $lock = CacheServices::lock($lockKey, true, 5);
        if (!$lock) {
            $this->error("提交过于频繁，请稍后再试");
        }
        if (!is_array($params['cartIds'])) {
            $params['cartIds'] = explode(',', $params['cartIds']);
        }
        $order = Db::transaction(function () use ($params) {
            return OrderServices::getInstance()->submit($this->auth->id, $params);
        });
        $this->success("提交成功", [
            'orderId' => $order->id
        ]);
    }

    // TODO 微信支付回调

    /**
     * 取消订单
     * @return void
     */
    public function cancel()
    {
        $orderId = input('orderId');
        if (empty($orderId)) {
            $this->error("订单号不能为空");
        };
        OrderServices::getInstance()->userCancel($this->auth->id, $orderId);
        $this->success();
    }

    /**
     * 申请退款
     * @return void
     */
    public function refund()
    {
        $orderId = input('orderId');
        if (empty($orderId)) {
            $this->error("订单号不能为空");
        };
        OrderServices::getInstance()->refund($this->auth->id, $orderId);
        $this->success();
    }

    /**
     * 确认收货
     * @return void
     */
    public function confirm()
    {
        $orderId = input('orderId');
        if (empty($orderId)) {
            $this->error("订单号不能为空");
        };
        OrderServices::getInstance()->confirm($this->auth->id, $orderId);
        $this->success();

    }

    /**
     * 删除订单
     * @return void
     */
    public function delete()
    {
        $orderId = input('orderId');
        if (empty($orderId)) {
            $this->error("订单号不能为空");
        };
        OrderServices::getInstance()->delete($this->auth->id, $orderId);
        $this->success();

    }

    public function detail()
    {
        $orderId = input('orderId');
        $result = OrderServices::getInstance()->detail($this->auth->id, $orderId);
        $this->success('成功',$result);
    }

=======
<?php

namespace app\api\controller\store;

//use AlibabaCloud\Client\Request\Request;
use app\admin\model\Evaluate;
use app\admin\model\user\Address;
use app\api\model\JoinMerchant;
use app\api\model\User;
use app\api\services\CacheServices;
use app\api\services\store\OrderServices;
use app\api\validate\SubmitOrderValidate;
use app\common\controller\Api;
use think\Db;
use think\Request;
use think\Queue;


class Order extends Api
{
    protected $noNeedRight = '*';
    protected $store_id;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
//        var_dump($this->auth->id);die;
        $this->store_id=JoinMerchant::where(['user_id'=>$this->auth->id])->value('id');
//
    }

    public function submit()
    {
        $params = input();
        (new SubmitOrderValidate())->goCheck();
        $lockKey = sprintf("order_submit_%s_%s", $this->auth->id, md5(serialize($params)));
        $lock = CacheServices::lock($lockKey, true, 5);
        if (!$lock) {
            $this->error("提交过于频繁，请稍后再试");
        }
        $order = Db::transaction(function () use ($params) {
            return OrderServices::getInstance()->submit($this->auth->id, $params);
        });
        $this->success("提交成功", [
            'orderId' => $order->id
        ]);
    }

    // TODO 微信支付回调
    public function cancel()
    {
        $orderId = input('orderId');
        if (empty($orderId)) {
            $this->error("订单号不能为空");
        };
        OrderServices::getInstance()->userCancel($this->auth->id, $orderId);
    }


    /**
     * 商家订单搜索
     * store/order/store_search
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function  store_search(Request $request){
        $post=$request->post();

         $where=[];
         if($post['search']){
             $where['a.order_sn']=['like','%'.$post['search'].'%'];

         }
        $where['a.store_id']=$this->store_id;
        $store=\app\api\model\Order::where($where)
            ->alias('a')
            ->join('user_address b','a.address_id=b.id')
            ->field('a.id,a.order_sn,a.order_status,a.actual_price,a.createtime,b.real_name,b.phone,b.detail,a.address_id')
            ->page($post['page'],$post['pagesize'])
            ->order('createtime desc')
            ->select();

        return json(['code'=>200,'msg'=>'请求成功','data'=>$store]);
    }


    /**
     * 商家订单列表
     * store/order/store_order_list
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function  store_order_list(Request $request){
        $post=$request->post();

        $where=[];
        $where['product_type']=$post['product_type']+1;
        if($post['status']>0){
            $where['status']=$post['status']-1;
        }
        if($where['product_type']==0){
            $where['a.store_id']=$this->store_id;
            $store=\app\api\model\Order::where($where)
                ->alias('a')
                ->join('user_address b','a.address_id=b.id')
                ->field('a.id,a.order_sn,a.order_status,a.actual_price,a.createtime,b.real_name,b.phone,b.detail,a.address_id')
                ->page($post['page'],$post['pagesize'])
                ->order('a.createtime desc')
                ->select();
        }
        if($where['product_type']==1){
            $where['a.store_id']=$this->store_id;
            $store=\app\api\model\Order::where($where)
                ->alias('a')
                ->field('a.id,a.order_sn,a.order_status,a.actual_price,a.coupon_price,a.createtime,a.lease_term,a.license_plate,a.message,a.refund_content,a.delivery_code')
                ->page($post['page'],$post['pagesize'])
                ->order('a.createtime desc')
                ->select();
        }
        if($where['product_type']==2){
            $where['a.store_id']=$this->store_id;
            $store=\app\api\model\Order::where($where)
                ->alias('a')
                ->join('user_address b','a.address_id=b.id')
                ->field('a.id,a.order_sn,a.order_status,a.actual_price,a.createtime,a.message,a.order_number,a.estimated_time,a.refund_content')
                ->page($post['page'],$post['pagesize'])
                ->order('a.createtime desc')
                ->select();
            foreach ($store as &$value){
                $value['estimated_delivery_time']='00:00';

            }
        }

        return json(['code'=>200,'msg'=>'请求成功','data'=>$store]);
    }

    /**
     * store/order/mall_orders
     * 商城订单详情
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function mall_orders(Request $request)
    {
      $post=$request->post();
        $store=\app\api\model\Order::where(['id'=>$post['order_id']])
            ->field('id,order_sn,order_status,actual_price,createtime,
            message,order_number,estimated_time,refund_content,address_id,
            specifications,message,goods_price,freight_price,coupon_price,order_price')
            ->find();
        $data['address']=Address::where(['id'=>$store['address_id']])->field('id,real_name,phone,detail')->find();
        $data['specifications']=json_encode($store['specifications'],1);
        $data['message']=$store['message'];
        $data['order_status']=$store['order_status'];
        $data['aftersale_status']=$store['aftersale_status'];
        $data['money']=[
              'goods_price'=>$store['goods_price'],
              'freight_price'=>$store['freight_price'],
              'coupon_price'=>$store['coupon_price'],
              'order_price'=>$store['order_price'],
        ];
        $data['time']=[
            'order_sn'=>$store['order_sn'],
            'createtime'=>date('Y-m-d H:i:s',$store['createtime']),
            'pay_time'=>$store['pay_time'],
            'ship_time'=>$store['ship_time'],
            'confirm_time'=>$store['confirm_time'],
        ];
        $data['refund']=[
            'refund_content'=>$store['refund_content'],
            'after_sales_type'=>$store['after_sales_type'],
            'refund_amount'=>$store['refund_amount'],
            'refund_type'=>$store['refund_type'],
            'refund_apply_time'=>$store['refund_apply_time'],
            'refund_time'=>$store['refund_time'],
            'describe_credentials'=>$store['describe_credentials'],
        ];
        return  json(['code'=>200,'msg'=>'请求成功','data'=>$data]);
    }

    /**
     * 商品评价列表
     * store/order/shop_evaluation_reply
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function shop_evaluation_reply(Request $request)
    {
        $post=$request->post();
        $Evaluate=Evaluate::where(['is_d'=>0,'order_id'=>$post['order_id'],'store_id'=>$this->store_id])
            ->field('id,user_image,user_name,start,content,images,createtime,store_content')
            ->page($post['page'],$post['pagesize'])
            ->order('createtime desc')
            ->select();
        foreach ($Evaluate as &$value){
            $value['user_image']=yuming($value['user_image']);
            $value['images']=yuming(explode(',',$value['images']),'');
        }

        return  json(['code'=>200,'msg'=>'请求成功','data'=>$Evaluate]);
    }

    /**
     * 商家回复评价
     * store/order/store_reply_to_evaluation
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function store_reply_to_evaluation(Request $request)
    {
        $post=$request->post();
        $Evaluate=Evaluate::where(['id'=>'evaluate_id'])->update(['store_content'=>$post['store_content'],'is_d'=>1,'hf_time'=>time()]);
        if($Evaluate){
            return  json(['code'=>200,'msg'=>'已回复','data'=>'']);
        }

    }

    /**
     * 商家点击发货
     * store/order/ship_now
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function ship_now(Request $request)
    {
        $post=$request->post();

        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update(['order_status'=>2,'ship_sn'=>$post['ship_sn'],'ship_time'=>date('Y-m-d H:i:s',time())]);
        if($Order){
            return  json(['code'=>200,'msg'=>'已发货','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'发货失败，请稍后再试','data'=>'']);
        }
    }
    /**
     * 商家拒绝发货
     * store/order/refusal_to_refund
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function refusal_to_refund(Request $request)
    {
        $post=$request->post();

        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update(['aftersale_status'=>4,'refusal_to_refund_text'=>$post['refusal_to_refund_text']]);
        if($Order){
            return  json(['code'=>200,'msg'=>'已拒绝','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'拒绝退款失败，请稍后再试','data'=>'']);
        }
    }

    /**
     * 商家同意退款
     * store/order/agree_to_refund
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function agree_to_refund(Request $request)
    {
        $post=$request->post();
        $zf_passwords=JoinMerchant::where(['id'=>$this->store_id])->value('zf_passwords');
        if($zf_passwords!=md5(md5($post['password']))){
            return  json(['code'=>404,'msg'=>'支付密码错误,请稍后再试，请稍后再试','data'=>'']);
        }
        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update(['aftersale_status'=>3]);
        if($Order){
            return  json(['code'=>200,'msg'=>'已退款','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'退款失败，请稍后再试','data'=>'']);
        }
    }

    /**
     * 租车订单详情
     * store/order/car_rental_orders
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     */
    public  function car_rental_orders(Request $request)
    {

        $post=$request->post();
        $store=\app\api\model\Order::where(['id'=>$post['order_id']])
            ->find()->toArray();
//      $data['address']=Address::where(['id'=>$store['address_id']])->field('id,real_name,phone,detail')->find();
        $data['specifications']=json_encode($store['specifications'],1);
        //车架号
        $data['frame_number']=$store['frame_number'];
        //提车码
        $data['delivery_code']=$store['delivery_code'];
        //租车类型
        $data['lease_type']=$store['lease_type'];
        //所在地区
        $data['lease_city']=$store['lease_city'];
        //详细地址
        $data['lease_address']=$store['lease_address'];
        //租期
        $data['lease_term']=$store['lease_term'];
        //联系电话
        $data['lease_mobile']=$store['lease_mobile'];
        $data['order_status']=$store['order_status'];
        $data['aftersale_status']=$store['aftersale_status'];
        $data['money']=[
            //押金
            'deposit'=>$store['deposit'],
            //商品总费用
            'goods_price'=>$store['goods_price'],
            //优惠金额
            'coupon_price'=>$store['coupon_price'],
            //总金额
            'order_price'=>$store['order_price'],
        ];
        $data['time']=[
            'order_sn'=>$store['order_sn'],
            'createtime'=>date('Y-m-d H:i:s',$store['createtime']),
            'pay_time'=>$store['pay_time'],
            'delivery_time'=>date('Y-m-d H:i:s',$store['delivery_time']),
            'return_time'=>date('Y-m-d H:i:s',$store['return_time']),
        ];
        $data['refund']=[
            'refund_content'=>$store['refund_content'],
            'after_sales_type'=>$store['after_sales_type'],
            'refund_amount'=>$store['refund_amount'],
            'refund_type'=>$store['refund_type'],
            'refund_apply_time'=>$store['refund_apply_time'],
            'refund_time'=>$store['refund_time'],
            'describe_credentials'=>$store['describe_credentials'],
        ];
        return  json(['code'=>200,'msg'=>'请求成功','data'=>$data]);

    }

    /**
     * 商家电池订单
     * store/order/battery_order_details
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function  battery_order_details(Request $request)
    {
        $post=$request->post();
        $store=\app\api\model\Order::where(['id'=>$post['order_id']])
            ->find()->toArray();
        //用户地址
        $data['address']=Address::where(['id'=>$store['address_id']])->field('id,real_name,phone,detail')->find()->toArray();
        //骑手信息
        $data['order_taker']=User::where(['id'=>$store['q_user_id']])->field('id,nickname,mobile,avatar,star')->find()->toArray();

        //商品信息
        $data['specifications']=json_encode($store['specifications'],1);
        //取货单号
        $data['pickup_note_number']=$store['pickup_note_number'];
        //预计送达时间
        $data['estimated_delivery_time']='00:00';
        //买家留言
        $data['message']=$store['message'];
        $data['money']=[
            //商品总费用
            'goods_price'=>$store['goods_price'],
            //优惠金额
            'coupon_price'=>$store['coupon_price'],
            //总金额
            'order_price'=>$store['order_price'],
        ];
        //时间
        $data['time']=[
            'order_sn'=>$store['order_sn'],
            'createtime'=>date('Y-m-d H:i:s',$store['createtime']),
            'pay_time'=>$store['pay_time'],
            'order_acceptance_time'=>date('Y-m-d H:i:s',$store['order_acceptance_time']),
            'pickup_time'=>date('Y-m-d H:i:s',$store['pickup_time']),
            'delivery_time'=>date('Y-m-d H:i:s',$store['delivery_time']),
        ];
        //退款信息
        $data['refund']=[
            'refund_content'=>$store['refund_content'],
            'after_sales_type'=>$store['after_sales_type'],
            'refund_amount'=>$store['refund_amount'],
            'refund_type'=>$store['refund_type'],
            'refund_apply_time'=>$store['refund_apply_time'],
            'refund_time'=>$store['refund_time'],
            'describe_credentials'=>$store['describe_credentials'],
        ];
        return  json(['code'=>200,'msg'=>'请求成功','data'=>$data]);
    }

>>>>>>> 0591f0f6f4619c1e24959052aec247fc0f331c0b
}